package com.campsys.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.campsys.core.Result;
import com.campsys.core.TableData;
import com.campsys.domain.TManagerActivity;
import com.campsys.domain.TManagerTeacher;
import com.campsys.domain.param.PageParam;
import com.campsys.domain.param.TeacherAddParam;
import com.campsys.domain.param.TeacherUpdateParam;
import com.campsys.domain.vo.ActivityPageVo;
import com.campsys.domain.vo.TeacherListVo;
import com.campsys.domain.vo.TeacherPageVo;
import com.campsys.mapping.ActivityMapping;
import com.campsys.mapping.TeacherMapping;
import com.campsys.service.ITeacherService;
import io.netty.util.internal.StringUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.parameters.P;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "教师管理")
@RestController
@RequestMapping("/teacher")
public class TeacherController {

    @Autowired
    private ITeacherService teacherService;

    @ApiOperation("添加教师")
    @PreAuthorize("@lcc.myAuthority('teacher:add')")
    @PostMapping("/add")
    public Result addTeacher(@Validated @RequestBody TeacherAddParam teacherAddParam){

        boolean rs = teacherService.addTeacher(teacherAddParam);
        return rs ? Result.success() : Result.error();
    }

    @ApiOperation("删除教师")
    @PreAuthorize("@lcc.myAuthority('teacher:delete')")
    @PutMapping("/delete/{id}")
    public Result deleteTeacher(@PathVariable Long id){

        boolean rs = teacherService.deleteTeacher(id);

        return rs ? Result.success() : Result.error();
    }

    @ApiOperation("修改教师信息")
    @PreAuthorize("@lcc.myAuthority('teacher:update')")
    @PutMapping("/update")
    public Result updateTeacher(@Validated @RequestBody TeacherUpdateParam teacherUpdateParam){

        TManagerTeacher tManagerTeacher = TeacherMapping.INSTANCE.to(teacherUpdateParam);

        boolean rs = teacherService.updateById(tManagerTeacher);

        return rs ? Result.success() : Result.error();
    }

    @ApiOperation("查询教师列表")
    @PreAuthorize("@lcc.myAuthority('teacher:list')")
    @GetMapping("/list")
    public Result getTeacherList(){

        LambdaQueryWrapper<TManagerTeacher> qw = new LambdaQueryWrapper<>();
        qw.ne(TManagerTeacher::getStatus,1l);

        List<TeacherListVo> list = TeacherMapping.INSTANCE.toList(teacherService.list(qw));

        return Result.success(list);
    }

    @ApiOperation("查询教师分页列表")
    @PreAuthorize("@lcc.myAuthority('teacher:page')")
    @GetMapping("/page")
    public TableData getTeacherPage(@Validated PageParam pageParam, String name){

        LambdaQueryWrapper<TManagerTeacher> qw = new LambdaQueryWrapper<>();
        qw.like(!StringUtil.isNullOrEmpty(name),TManagerTeacher::getTeacherName,name);
        qw.ne(TManagerTeacher::getStatus,1l);

        Page<TManagerTeacher> teacherPage = new Page<>(pageParam.getPageNum(),pageParam.getPageSize());

        Page<TManagerTeacher> page = teacherService.page(teacherPage,qw);

        List<TeacherPageVo> list = TeacherMapping.INSTANCE.toPage(page.getRecords());

        return TableData.success(list,page.getTotal());

    }

    @ApiOperation("根据loginId获取teacherId")
    @PreAuthorize("@lcc.myAuthority('teacher:get:id')")
    @GetMapping("/get/id/{userId}")
    public Result getTeacherId(@PathVariable Long userId){

        LambdaQueryWrapper<TManagerTeacher> qw = new LambdaQueryWrapper<>();
        qw.eq(TManagerTeacher::getLoginId,userId);

        TManagerTeacher tManagerTeacher = teacherService.getOne(qw);

        return Result.success(tManagerTeacher.getId());
    }


}
